package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.json.JSONObject;
import com.ruoyi.mall.domain.vo.MallGoodsCategoryVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.MallGoodsVo;
import com.ruoyi.mall.domain.bo.MallGoodsBo;
import com.ruoyi.mall.service.IMallGoodsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商城商品信息
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/goods")
public class MallGoodsController extends BaseController {

    private final IMallGoodsService iMallGoodsService;

    /**
     * 查询商城商品信息列表
     */
    @SaCheckPermission("mall:goods:list")
    @GetMapping("/list")
    public TableDataInfo<MallGoodsVo> list(MallGoodsBo bo, PageQuery pageQuery) {
        return iMallGoodsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商城商品信息列表
     */
    @SaCheckPermission("mall:goods:export")
    @Log(title = "商城商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MallGoodsBo bo, HttpServletResponse response) {
        List<MallGoodsVo> list = iMallGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商城商品信息", MallGoodsVo.class, response);
    }

    /**
     * 获取商城商品信息详细信息
     *
     * @param goodsId 主键
     */
    @SaCheckPermission("mall:goods:query")
    @GetMapping("/{goodsId}")
    public R<MallGoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long goodsId) {
        return R.ok(iMallGoodsService.queryById(goodsId));
    }

    /**
     * 新增商城商品信息
     */
    @SaCheckPermission("mall:goods:add")
    @Log(title = "商城商品信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MallGoodsBo bo) {
        return toAjax(iMallGoodsService.insertByBo(bo));
    }

    /**
     * 修改商城商品信息
     */
    @SaCheckPermission("mall:goods:edit")
    @Log(title = "商城商品信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MallGoodsBo bo) {
        return toAjax(iMallGoodsService.updateByBo(bo));
    }

    /**
     * 删除商城商品信息
     *
     * @param goodsIds 主键串
     */
    @SaCheckPermission("mall:goods:remove")
    @Log(title = "商城商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] goodsIds) {
        return toAjax(iMallGoodsService.deleteWithValidByIds(Arrays.asList(goodsIds), true));
    }

    /**
     * 获取部门树列表
     */
    @SaCheckPermission("mall:goods:list")
    @GetMapping("/categoryTree")
    public R<List<Tree<Long>>> categoryTree(MallGoodsCategoryVo goodsCategoryBo) {
        return R.ok(iMallGoodsService.selectCategoryTreeList(goodsCategoryBo));
    }

    /**
     * 根据商品ID获取商品完整信息
     */
    @SaIgnore
    @PostMapping("/getGoodsById")
    public R<MallGoodsVo> getGoodsById(@RequestBody JSONObject json)
    {
        Long id = Long.valueOf(json.get("id").toString());
        return R.ok(iMallGoodsService.queryById(id));
    }

    /**
     * 根据商品分类ID获取商品信息列表
     * @param json
     * @return
     */
    @SaIgnore
    @RequestMapping("/getGoodsByCategoryId")
    public R<List<MallGoodsVo>> getGoodsByCategoryId(@RequestBody JSONObject json)
    {
        Long categoryId = Long.valueOf(json.get("categoryId").toString());
        MallGoodsBo query = new MallGoodsBo();
        query.setCategoryId(categoryId);
        return R.ok(iMallGoodsService.queryList(query));
    }
}
